package desktop.playerguide;

import pulpcore.Input;
import pulpcore.Stage;
import pulpcore.image.filter.Blur;
import pulpcore.sprite.ImageSprite;
import pulpcore.sprite.Label;
import desktop.playerguide.aboutmodel.About;
import desktop.playerguide.multichoiceswin.ChoiceForm;
import desktop.playerguide.splashmodel.Splash;

public class Choice extends ImageSprite {

	Label label;
	ChoiceForm myForm;
	int filterIndex = 0;
	Blur blur = new Blur();
	boolean hasPending = false;
	boolean isChoiceView = true;

	public Choice(String image, Label label) {
		super(image, 0, 0);
		this.label = label;
		this.label.alpha.set(0);
		setFilter(blur);
		setAnchor(0.5, 0.5);
		setCursor(Input.CURSOR_HAND);
	}

	@Override
	public void update(int elapsedTime) {
		super.update(elapsedTime);

		if (isChoiceView) {
			this.doCircumvolve(elapsedTime);
		} else if (!isChoiceView && hasPending) {
			myForm.update(elapsedTime);
		}

		if (isMousePressed()) {
			isChoiceView = false;
			if (isNeedPendingMenuChoice()) {
				pendingMenu();
				hasPending = true;
			} else if (this.label.getText().equals("aboutModel")) {
				Stage.replaceScene(new About());
			} else {
				Stage.replaceScene(new Splash());
			}
		}
	}

	private void doCircumvolve(int elapsedTime) {
		int blurGoal = isMouseOver() ? 0 : 10;
		if (!blur.radius.isAnimating() && blur.radius.get() != blurGoal) {
			int dur = 100 + blurGoal * 20;
			blur.radius.animateTo(blurGoal, dur);
			label.alpha.animateTo(blurGoal == 0 ? 255 : 0, dur);
		}
	}

	private boolean isNeedPendingMenuChoice() {
		return ((label.getText()).equals("desktopMode") || (label.getText())
				.equals("webModel")) ? true : false;
	}

	// 产生选择界面表单
	private void pendingMenu() {
		myForm = new ChoiceForm(label.getText());
	}
}